/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.agent.controller.notice;

import com.shop.cereshop.agent.page.notice.CereNoticeAgentVO;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.agent.param.notice.NoticeDTO;
import com.shop.cereshop.agent.service.notice.CereNoticeService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.notice.CereNotice;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 消息中心
 */
@RestController
@RequestMapping("notice")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "NoticeController")
@Api(value = "消息中心", tags = "消息中心")
public class CereNoticeController {

  @Autowired
  private CereNoticeService cereNoticeService;

  /**
   * 消息详情查询
   *
   * @return
   */
  @GetMapping(value = "getById")
  @ApiOperation(value = "消息详情查询")
  public Result<CereNoticeAgentVO> getById(
      @RequestParam @ApiParam(value = "消息id", required = true) Long noticeId,
      HttpServletRequest request) throws CoBusinessException {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    CereNoticeAgentVO cereNotice = cereNoticeService.getByIdForAgent(noticeId, user.getAgentUserId());
    return new Result(cereNotice, CoReturnFormat.SUCCESS);
  }

  /**
   * 代理商消息列表查询接口
   *
   * @return
   */
  @GetMapping(value = "agent/getAll")
  @ApiOperation(value = "代理商消息列表查询接口")
  public Result<Page<CereNoticeAgentVO>> getAgentNotices(
      @RequestParam("page") @ApiParam(value = "页码", required = true) Integer page,
      @RequestParam("pageSize") @ApiParam(value = "分页大小", required = true) Integer pageSize,
      @RequestParam("noticeType") @ApiParam(value = "消息类型 1-系统消息（系统自动发送） 2-公告（后台录入） 3-站内信", required = true) Integer noticeType,
      HttpServletRequest request) throws CoBusinessException {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    Page<CereNoticeAgentVO> result = cereNoticeService.getAll(page, pageSize, user.getAgentUserId(), noticeType,
        IntegerEnum.NOTICE_RECEIVE_AGENT.getCode());
    return new Result(result, CoReturnFormat.SUCCESS);
  }

  /**
   * 代理商消息列表查询接口
   *
   * @return
   */
  @GetMapping(value = "salesman/getAll")
  @ApiOperation(value = "业务员消息列表查询接口")
  public Result<Page<CereNoticeAgentVO>> getSalesmanNotices(
      @RequestParam("page") @ApiParam(value = "页码", required = true) Integer page,
      @RequestParam("pageSize") @ApiParam(value = "分页大小", required = true) Integer pageSize,
      @RequestParam("noticeType") @ApiParam(value = "消息类型 1-系统消息（系统自动发送） 2-公告（后台录入） 3-站内信", required = true) Integer noticeType,
      HttpServletRequest request) throws CoBusinessException {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    Page<CereNoticeAgentVO> result = cereNoticeService.getAll(page, pageSize, user.getAgentUserId(), noticeType,
        IntegerEnum.NOTICE_RECEIVE_AGENT.getCode());
    return new Result(result, CoReturnFormat.SUCCESS);
  }

  /**
   * 首页通知接口
   *
   * @return
   */
  @GetMapping(value = "agent/getIndexNotice")
  @ApiOperation(value = "代理商首页通知查询接口")
  public Result<CereNoticeAgentVO> getAgentIndexNotice(HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(cereNoticeService.getIndexNoticeForAgent(IntegerEnum.NOTICE_RECEIVE_AGENT.getCode(), user));
  }

  @GetMapping(value = "salesman/getIndexNotice")
  @ApiOperation(value = "业务员首页通知查询接口")
  public Result<CereNoticeAgentVO> getSalesmanIndexNotice(HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(cereNoticeService.getIndexNoticeForAgent(IntegerEnum.NOTICE_RECEIVE_SALESMAN.getCode(), user));
  }
}
